#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "mhbtn.h"
#include "func.h"
#include "com.h"

/*
 *
 */
static int MyControlProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
    {
    case MSG_ERASEBKGND:
        {
            RECT rect;
        	GetClientRect(hWnd, &rect);
            HDC hdc = GetClientDC(hWnd);
            DWORD nStyle = GetWindowStyle(hWnd);
            int nData = GetWindowAdditionalData(hWnd);
            if(nStyle & BS_FLAT)
            {
                int nWidth = GET_RECT_WIDTH(rect);
                int nHeight = GET_RECT_HEIGHT(rect);
                if(nData & MH_BTN_BKGND_BLACK)
                {
                    SetBrushColor(hdc, COLOR_HQ_BLACK(hdc));
                }
                else if(nData & MH_BTN_BKGND_GREEN)
                {
                    SetBrushColor(hdc, COLOR_HQ_GREEN(hdc));
                }
                else
                {
                    SetBrushColor (hdc, COLOR_HQ_BKGND(hdc));
                }
                if(lParam)
                {
                    RECT rct;
                    memcpy(&rct, (RECT*)lParam, sizeof(RECT));
                    int w = GET_RECT_WIDTH(rct);
                    int h = GET_RECT_HEIGHT(rct);
                    FillBox(hdc, rct.left, rct.top, w, h);
                }
                else
                {
                    FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                }
            }
            else
            {	
                rect.left += 1;
                rect.right -= 2;
                rect.top += 1;
                rect.bottom -= 2;
                int nWidth = GET_RECT_WIDTH(rect);
                int nHeight = GET_RECT_HEIGHT(rect);
                if(nData & STATE_BTN_DOWN)
                {
                    SetBrushColor(hdc, COLOR_HQ_YELLOW(hdc));
                }
                {
                    SetBrushColor(hdc, COLOR_HQ_BKGND(hdc));
                }
                if (lParam)
                {
                    RECT rct;
                    memcpy(&rct, (RECT*) lParam, sizeof(RECT));
                    int w = GET_RECT_WIDTH(rct);
                    int h = GET_RECT_HEIGHT(rct);
                    FillBox(hdc, rct.left, rct.top, w, h);
                }
                else
                {
                    FillBox(hdc, rect.left, rect.top, nWidth, nHeight);
                }
                if(1)
                {
                    SetPenColor(hdc, RGB2Pixel(hdc, 29, 46, 117));
                    MoveTo(hdc, rect.left, rect.bottom);
                    LineTo(hdc, rect.right, rect.bottom);
                    LineTo(hdc, rect.right, rect.top);
                    MoveTo(hdc, rect.left - 1, rect.bottom + 1);
                    LineTo(hdc, rect.right + 1, rect.bottom + 1);
                    LineTo(hdc, rect.right + 1, rect.top + 1);
                    SetPenColor(hdc, RGB2Pixel(hdc, 153, 221, 226));
                    MoveTo(hdc, rect.left, rect.bottom);
                    LineTo(hdc, rect.left, rect.top);
                    LineTo(hdc, rect.right, rect.top);
                    MoveTo(hdc, rect.left - 1, rect.bottom + 1);
                    LineTo(hdc, rect.left - 1, rect.top + 1);
                    LineTo(hdc, rect.right + 1, rect.top + 1);
                }
            }
            if(nData & MH_BTN_GRAY_BORDER)
            {
                SetPenColor(hdc, COLOR_HQ_GRAY(hdc));
                Rectangle(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1);
            }
            else if(nData & MH_BTN_BLACK_BORDER)
            {
                SetPenColor(hdc, COLOR_HQ_BLACK(hdc));
                Rectangle(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1);
            }
        	ReleaseDC(hdc);
        }
        return 0;
    case MSG_LBUTTONDOWN:
        {
        	if (GetCapture () == hWnd)
        	{
        		break;
        	}
        	SetCapture (hWnd);
        	int nData = GetWindowAdditionalData(hWnd);
        	nData |= STATE_BTN_DOWN;
        	SetWindowAdditionalData(hWnd, nData);
        	InvalidateRect (hWnd, NULL, TRUE);
        }
        break;
    case MSG_LBUTTONUP:
        {
        	if (GetCapture() != hWnd)
        	{
        		break;
        	}
        	ReleaseCapture ();
        	int nData = GetWindowAdditionalData(hWnd);
        	nData &= ~STATE_BTN_DOWN;
        	InvalidateRect (hWnd, NULL, TRUE);
        	HWND hParent = GetParent(hWnd);
        	int nID = GetDlgCtrlID(hWnd);
        	SendMessage(hParent, MSG_COMMAND, nID, 0);
        }
    	break;
    case MSG_PAINT:
        {
        	RECT rect;
        	GetClientRect(hWnd, &rect);
        	DWORD nStyle = GetWindowStyle(hWnd);
        	HDC hdc = BeginPaint(hWnd);
        	SetBkMode(hdc, BM_TRANSPARENT);
        	int nData = GetWindowAdditionalData(hWnd);
        	if(nData & MH_BTN_GRAY_BORDER)
        	{
        	    rect.left += 2;
        	    rect.right -= 3;
        	}
            int nFormat = DT_VCENTER | DT_SINGLELINE;
            if(nStyle & BS_LEFT)
            {
                nFormat |= DT_LEFT;
            }
            else if(nStyle & BS_RIGHT)
            {
                nFormat |= DT_RIGHT;
            }
            else
            {
                nFormat |= DT_CENTER;
            }
            if (GetFocusChild(GetParent(hWnd)) == hWnd)
            {
                SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            }
            else
            {
                if(nData & MH_BTN_TEXT_WHITE)
                {
                    SetTextColor(hdc, COLOR_HQ_WHITE(hdc));
                }
                else if(nData & MH_BTN_TEXT_GREEN)
                {
                    SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
                }
                else if(nData & MH_BTN_TEXT_RED)
                {
                    SetTextColor(hdc, COLOR_HQ_RED(hdc));
                }
                else if(nData & MH_BTN_BKGND_BLACK)
                {
                    SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
                }
                else if(nData & MH_BTN_TEXT_BLACK)
                {
                    SetTextColor(hdc, COLOR_HQ_BLACK(hdc));
                }
                else if(nData & MH_BTN_TEXT_CYAN)
                {
                    SetTextColor(hdc, COLOR_HQ_CYAN(hdc));
                }
                else
                {
                    SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
                }
            }
        	DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, nFormat);
        	EndPaint(hWnd, hdc);
        }
        break;
    case MSG_CREATE:
        {
        }
    	break;
    case MSG_DESTROY:
        {
        }
        break;
    case MSG_SETFOCUS:
        InvalidateRect (hWnd, NULL, TRUE);
        break;
    case MSG_KILLFOCUS:
        InvalidateRect (hWnd, NULL, TRUE);
        break;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}

BOOL RegisterMhBtn (void)
{
    WNDCLASS MyClass;

    MyClass.spClassName = MH_BTN_NAME;
    MyClass.dwStyle     = WS_NONE;
    MyClass.dwExStyle   = WS_EX_NONE;
    MyClass.hCursor     = GetSystemCursor (IDC_ARROW);
    MyClass.iBkColor    = COLOR_lightwhite;
    MyClass.WinProc     = MyControlProc;

    return RegisterWindowClass (&MyClass);
}

void UnregisterMhBtn (void)
{
    UnregisterWindowClass (MH_BTN_NAME);
}

